<?php
/*
 * 权限组
 */
class Module extends Auth_Controller {
	function __construct(){
		parent::__construct();
//		$this->myauth->execute_auth ();
		$this->load->model('acl/PermissionModel','',true) ;
		$this->load->model('acl/ModuleModel','',true);
	}
	/**
	 * 权限组列表
	 */
	function index(){
		$data['list'] = $this->ModuleModel->selectAll();
		$data['action'] = 'list';
		$this->_view('manager/acl/module',$data);
	}
	function isExistName($name) {
		$name = urldecode($name);
		$data['state'] = $this->ModuleModel->count(array('name'=>$name));
		$this->_output_json($data);
	}
	/**
	 * 新添权限分类
	 */
	function add (){
		$data['action'] = 'add';
		$this->_view('manager/acl/module',$data);
	}
	
	/**
	 * 执行权限分类添加
	 */
	function doadd (){
		$module = $this->input->post('module', true ) ;
		if($module['sort'] == '')
			$module['sort'] = 0;
		$mid = $this->ModuleModel->insert($module);
		$permissions = $this->input->post('permission', true ) ;
		if($mid > 0){
			foreach ($permissions as $permission) {
				if($permission['sort'] == ''){
					$permission['sort'] = 0;
				}
				$permission['mid'] = $mid;
				$this->PermissionModel->insert($permission);
			}
		}
		$data['state'] = $mid;
		$data['message'] = '新增权限模块与权限'.($mid > 0?'成功。':'失败！');
		$this->_output_json($data);
//		$this->_view('manager/acl/module',$data);
		
//		$data['module']['sortid']  = $this->input->post("sortid" , true ) ;
//		$this->form_validation->set_rules($this->setRules());
//		if ($this->form_validation->run() == FALSE){
//			$this->load->view('acl/module_list',$data);
//		}else{
//			if($this->ModuleModel->exist_module_name($data['module']['names'],null)){
//				echo "exits" ;
//			}else{
//				$flag = $this->ModuleModel->add_module($data['module']);
//				if(!$flag){
//					echo "falid" ;
//				}else{
//					$data['list'] = $this->ModuleModel->get_module_all();
//					$this->load->view('acl/module_list',$data);
//				}
//			}
//		}
	}

	/**
	 * 修改权限模块
	 */
	function edit ($mid=0){
		$data['module'] = $this->ModuleModel->selectById($mid);
		$data['permissions'] = $this->PermissionModel->selectByParam( array('mid'=>$mid),'sort asc');
		$data['action'] = "edit";
		$this->_view('manager/acl/module',$data);
	}
	
	function doedit () {
		$module = $this->input->post('module', true ) ;
		if($module['sort'] == '')
			$module['sort'] = 0;
		$r = $this->ModuleModel->update($module);
		$permissions = $this->input->post('permission', true ) ;
		$pids = $this->input->post('pid', true ) ;
		if($r >= 0){
			if(is_array($pids))
				$this->PermissionModel->deleteByIds($pids);
			if(is_array($permissions))
				foreach ($permissions as $permission) {
					if($permission['sort'] == ''){
						$permission['sort'] = 0;
					}
					if($permission['pid'] == 0){
						$permission['mid'] = $module['mid'];
						$this->PermissionModel->insert($permission);
					}else{
						$this->PermissionModel->update($permission);
					}
				}
		}
		
		$data['state'] = $r;
		$data['message'] = '编辑权限模块与权限'.($r >= 0?'成功。':'失败！');
		$this->_output_json($data);
	}
	/**
	 * 权限分类删除
	 */
	function dodelete ($mid=0) {
		$total = $this->PermissionModel->count(array('mid'=>$mid));
		$r = -1;
		if($total == 0){
			$r = $this->ModuleModel->delete($mid);
		}
//		$data['state'] = $r;
//		$data['message'] = '删除权限模块'.($r > 0?'成功。':('失败！'.$r == -1?'(模块下存在权限)':''));
		redirect('manager/acl/module');
	}
	

	/**
	 * 验证新添权限分类
	 */
	function setRules()
	{
		return array(
		array(
				"label"=>"组名",
				"field"=>"names",
				"rules"=>"required"
				),array(
				"label"=>"排序",
				"field"=>"sortid",
				"rules"=>"required|numeric"
				)
				);
	}
}
?>
